import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const VueRouterPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return VueRouterPush.call(this, location, onResolve, onReject)
    return VueRouterPush.call(this, location).catch(err => err)
}

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true
    },
    {
        path: '/404',
        component: () => import('@/views/error-page/404'),
        hidden: true
    },
    {
        path: '/403',
        component: () => import('@/views/error-page/403'),
        hidden: true
    },
    {
        path: '/500',
        component: () => import('@/views/error-page/500'),
        hidden: true
    },
    {   path: '/redirect',
        component: () => import('@/views/login/redirect'), 
        hidden: true
    },
    {
        path: '/',
        redirect: '/workplace',
        hidden: true
    },
    {
        path: '/public',
        component: Layout,
        name: 'public',
        hidden: true,
        children: [
            {
                path: '/user/center',
                component: () => import('@/views/user/center'),
                name: 'userCenter',
                meta: { title: '用户中心', index: '1-1'},
                hidden: true
            }
        ]
    },
    {
        path: '/workplace',
        component: Layout,
        meta: { title: '工作台', icon: 'dashboard', affix: true },
        redirect: '/workplace',
        children: [
            {
                path: '/workplace',
                component: () => import('@/views/dashboard/index'),
                name: 'workplace',
                meta: { title: '工作台', icon: 'documentation', affix: true }
            }
        ]
    },
    

]

export const asyncRoutes = [
    {
        path: '/product',
        component: Layout,
        meta: { title: '商品管理', icon: 'shopping', affix: true },
        children: [
            {
                path: '/product',
                component: () => import('@/views/product/index'),
                name: 'product',
                meta: { title: '商品信息', icon: 'user', affix: true }
            },
            {
                path: '/product/barcode',
                component: () => import('@/views/product/barcode'),
                name: 'barcode',
                meta: { title: '商品条码', icon: 'user', affix: true, roles: [1, 9] }
            },
            {
                path: '/barcode/report',
                component: () => import('@/views/product/report'),
                name: 'report',
                meta: { title: '取用报表', icon: 'user', affix: true, roles: [1, 9] }
            }
        ]
    },
    {
        path: '/order',
        component: Layout,
        meta: { title: '订单管理', icon: 'ordered-list', affix: true },
        children: [
            {
                path: '/order/list',
                component: () => import('@/views/order/index'),
                name: 'order',
                meta: { title: '全部的订单', icon: 'user', affix: true }
            },
            {
                path: '/order/unconfirmed',
                component: () => import('@/views/order/index'),
                name: 'unconfirmedOrder',
                meta: { title: '待确认订单', icon: 'user', affix: true },
                query: "status=0"
            },
            {
                path: '/order/unshipped',
                component: () => import('@/views/order/index'),
                name: 'unshippedOrder',
                meta: { title: '待发货订单', icon: 'user', affix: true },
                query: "status=1"
            },
            {
                path: '/order/unshoping',
                component: () => import('@/views/order/index'),
                name: 'unshopingOrder',
                meta: { title: '待收货订单', icon: 'user', affix: true },
                query: "status=2"
            },
            {
                path: '/order/done',
                component: () => import('@/views/order/index'),
                name: 'doneOrder',
                meta: { title: '已完成订单', icon: 'user', affix: true },
                query: "status=3"
            },
            {
                path: '/order/cancel',
                component: () => import('@/views/order/index'),
                name: 'cancelOrder',
                meta: { title: '已取消订单', icon: 'user', affix: true },
                query: "status=4"
            }
        ]
    },
    {
        path: '/address',
        component: Layout,
        meta: { title: '地址管理', icon: 'flag', affix: true, roles: [1, 9] },
        children: [
            {
                path: '/address',
                component: () => import('@/views/address/index'),
                name: 'address',
                meta: { title: '地址管理', icon: 'flag', affix: true, roles: [1, 9] }
            }
        ]
    },
    {
        path: '/notice',
        component: Layout,
        meta: { title: '消息中心', icon: 'notification', affix: true },
        children: [
            {
                path: '/notice',
                component: () => import('@/views/notice/index'),
                name: 'notice',
                meta: { title: '消息中心', icon: '', affix: true }
            }
        ]
    },
    {
        path: '/log',
        component: Layout,
        meta: { title: '日志管理', icon: 'history', affix: true, roles: [9] },
        children: [
            {
                path: '/log/login',
                component: () => import('@/views/sys-log/login'),
                name: 'log-login',
                meta: { title: '登录日志', icon: '', affix: true, roles: [9] }
            },
            {
                path: '/log/operate',
                component: () => import('@/views/sys-log/operate'),
                name: 'log-operate',
                meta: { title: '操作日志', icon: '', affix: true, roles: [9] }
            }
        ]
    },
    {
        path: '/user',
        component: Layout,
        meta: { title: '用户管理', icon: 'user', affix: true, roles: [9] },
        children: [
            {
                path: '/user',
                component: () => import('@/views/user/index'),
                name: 'user',
                meta: { title: '用户管理', icon: 'user', affix: true, roles: [9] }
            }
        ]
    }
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
